package services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import play.mvc.Http.Response;

public class Report {

	public static byte[] generate(String template, String fileName,
			Map<String, Object> data, List<?> datasource, Response response)
			throws JRException {

		if (datasource == null)
			datasource = new ArrayList<Object>();
		JRBeanCollectionDataSource x = new JRBeanCollectionDataSource(
				datasource);

		byte[] xy = JasperRunManager.runReportToPdf(template, data, x);
		response.setHeader("Content-Disposition", "attachment; filename="
				+ fileName);
		response.setContentType("application/pdf");

		return xy;
	}
}
